$(function () {
    //变量的抽取:
    var orderDataGrid=$('#orderDataGrid');
    var orderAddDialog=$('#orderAddDialog');
    var  orderAddDialogForm=$('#orderAddDialogForm');
     var billItems=$('#billItems');
    //统一绑定事件
    $("a[data-method]").click(function () {
        var method = $(this).data("method");
        methodObj[method]();
    });
    var methodObj = {


        search:function () {
            //获取关键字
            var keyWord = $("#keyWord").val();

            //查询
            orderDataGrid.datagrid("load",{"keyWord":keyWord});

            //获取关键字
            var beginDate = $("#beginDate").val();

            //查询
            orderDataGrid.datagrid("load",{"beginDate":beginDate});
            //获取关键字
            var endDate = $("#endDate").val();

            //查询
            orderDataGrid.datagrid("load",{"endDate":endDate});

            //获取关键字
            var payStateId = $("#payStateId").val();

            //查询
            orderDataGrid.datagrid("load",{"payStateId":payStateId});

            //获取关键字
            var payModeId = $("#payModeId").val();

            orderDataGrid.datagrid("load",{"payModeId":payModeId});

        },
        add:function () {
            //弹出添加的dialog
            orderAddDialogForm.form("clear");
            orderAddDialog.dialog("open").dialog("center");
        },
        edit:function () {

            //获取选中行
            var row = orderDataGrid.datagrid("getSelected");
            if(row){
                //表单数据回显
                orderAddDialogForm.form("clear");
                orderAddDialogForm.form('load',row);

                //弹出编辑框
                orderAddDialog.dialog("open").dialog("center");
            }else{
                //给出提示
                $.messager.alert("提示","你不选择数据我怎么给你修改？","error");
            }


        },
        remove:function () {

            //获取选中行
            var row = orderDataGrid.datagrid("getSelected");
            if(row){
                var id = row.id;
                var state = row.state;
                if(state == 1){
                    $.messager.alert("提示","状态已经是删除","error");
                }else{
                    //删除请求ajax
                    $.post("order/remove",{"id":id},function (data) {
                        if(data.success){
                            //列表刷新
                            $.messager.alert("提示","恭喜您,提交成功,列表已经刷新","info");
                            methodObj.reload();
                        }else{
                            $.messager.alert("提示","恭喜您,提交失败,错误原因："+data.msg,"error");
                        }
                    });
                }
            }else{
                //给出提示
                $.messager.alert("提示","你不选择数据我怎么给你删除？","error");
            }
        },
        reload:function () {



            orderDataGrid.datagrid("reload");
        },
        save:function () {
            //获取表单
            //提交表单
            orderAddDialogForm.form('submit', {
                url:"indent/saveOrUpdate",
                success:function(data){
                    data = $.parseJSON(data);
                    if(data.success){
                        //列表刷新
                        $.messager.alert("提示","恭喜您,提交成功,列表已经刷新","info");
                        methodObj.close();
                        methodObj.reload();
                    }else{
                        $.messager.alert("提示","恭喜您,提交失败,错误原因："+data.msg,"error");
                    }
                }
            });

        },
        close:function () {
            orderAddDialog.dialog("close");
        }

    }

    orderDataGrid.datagrid({
        url: 'indent/list',
        fit:true,
        title:'订单管理',
        rownumbers:true,
        singleSelect:true,
        pagination:true,
        toolbar:"#orderDataGridToolbar",
        columns: [[
            {field: 'orderNum', title: '订单编号', width: '5%',align:  'center'},
            {field: 'orderTime', title: '下单时间', width: '12%',align:  'center'},
            {field: 'productName', title: '商品名', width: '7%',align:  'center'},
            {field: 'productNum', title: '单价', width: '5%',align:  'center'},
            {field: 'productNum', title: '数量', width: '5%',align:  'center'},
            {field: 'productAmount', title: '金额', width: '5%',align:  'center'},
            {field: 'totalNum', title: '总数量', width: '6%',align:  'center'},
            {field: 'totalPrice', title: '总金额', width: '6%',align:  'center' } ,
            {field: 'memberPhoneNum', title: '手机号码', width: '11%',align:  'center'},
            {field: 'memberCardId', title: '会员号', width: '15%',align:  'center' } ,
            {field: 'payStateId', title: '订单状态', width: '6%',align:  'center',formatter:function (v,r) {
                    if (v == 1) {
                        return "<span style='color: #01AAED'>已支付</span>"
                    } else if (v == 2) {
                        return "<span style='color: #00FF00'>未支付</span>"
                    }
                }},
            {field: 'payModeId', title: '付款方式', width: '9%',align:  'center',formatter:function (v,r) {
                    if (v == 1) {
                        return "<span style='color: #01AAED'>微信支付</span>"
                    } else if (v == 2) {
                        return "<span style='color: #00FF00'>储值卡</span>"
                    } else {
                        return "<span style='color: #c9302c'>未支付</span>"
                    }
                }},
            {field: 'operatorName', title: '操作人', width: '10%',align:  'center'}


        ]]
    })
        billItems.datagrid({


        })

    ;
    //添加的dialog
    orderAddDialog.dialog({
        closed: true,
        modal: true,
        buttons:"#orderAddDialogButtons"
    })
}


);